package com.piece.module.file.util.pdf.event;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.module.file.util.pdf.BuilderContext;
import com.piece.module.file.util.pdf.PdfColor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WaterMarIEvent implements IEventHandler {

    private BuilderContext builderContext;
    protected PdfFont font;

    public WaterMarIEvent(PdfFont font, BuilderContext builderContext) {
        this.font = font;
        this.builderContext = builderContext;
    }

    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfPage page = docEvent.getPage();
        Rectangle pageSize = page.getPageSize();
        if (StringUtil.isNotEmpty(builderContext.getWaterMark())) {
            drawWaterMarText(page, pageSize);
        }
        if (null != builderContext.getBackground()) {
            drawWaterMarBackground(page, pageSize);
        }
    }

    private void drawWaterMarText(PdfPage page, Rectangle pageSize) {
        Rectangle rectangle = new Rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
        Paragraph paragraph = new Paragraph()
                .add(builderContext.getWaterMark())
                .setWidth(pageSize.getWidth())
                .setHeight(pageSize.getHeight())
                .setTextAlignment(TextAlignment.CENTER)
                .setVerticalAlignment(VerticalAlignment.MIDDLE)
                .setRotationAngle(45)
                .setOpacity(0.4f);
        Canvas canvas = new Canvas(page, rectangle)
                .setFont(font)
                .setFontColor(PdfColor.getDarkGreyColor())
                .setFontSize(100)
                .showTextAligned(paragraph, pageSize.getWidth() / 2 - 50, 480, TextAlignment.CENTER, VerticalAlignment.MIDDLE);
        canvas.flush();
        canvas.close();
    }

    private void drawWaterMarBackground(PdfPage page, Rectangle pageSize) {
        try {
            Image image = new Image(ImageDataFactory.create(builderContext.getBackground()));
            image.setWidth(pageSize.getWidth());
            image.setHeight(pageSize.getHeight());
            image.setOpacity(0.4f);
            Rectangle rectangle = new Rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
            Canvas canvas = new Canvas(page, rectangle);
            canvas.add(image).flush();
            canvas.close();
        } catch (Exception e) {
            log.error("添加Pdf页眉Logo错误：{}", e.getMessage());
        }
    }
}
